<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/20 0020
 * Time: 23:03
 */

namespace app\Lib\Model\Login;


use app\Lib\Api\Login\validateLogin;
use app\Lib\Api\Login\weiXinLogin;
use app\Lib\Exception\BlException;
use app\Lib\TopClient;
use think\Cache;
use think\Cookie;
use think\Exception;
use think\Session;

class DoLogin
{
    private $account;
    private $password;
    private $prefix = 'anbang';
    private $loginKey = 'admin_web_log_key';
    private $loginTime = '604800';
    private $openId;
    public function setOpenId($openId = ''){
        $this->openId = $openId;
    }
    /**
     * @param mixed $account
     * @return DoLogin
     */
    public function setAccount($account)
    {
        $this->account = $account;
        return $this;
    }

    /**
     * @param mixed $password
     * @return DoLogin
     */
    public function setPassword($password)
    {
        $this->password = $password;
        return $this;
    }

    public function setLoginKey($loginKey){
        $this->loginKey = $loginKey;
    }

    /**
     * @return array|string
     * @throws \Exception
     */
    public function run(){
        try{
            $model = new \app\Lib\Api\Login\doLogin();
            $model->setArgs('account',$this->account);
            $model->setArgs('password',$this->getPassword());
            $model->setArgs('loginIp','127.0.0.1');
            $result = (new TopClient($model))->getSuccessData();
            Session::set('login_uid',$result['uid']);
            Session::set('userInfo',$result);
            if(!isset($result['userToken']) || empty($result['userToken'])){
                throw new Exception('前端登录失败');
            }
            cache($result['userToken'],'loginIng',$this->loginTime);
            Cookie::set($this->getLoginKey(),$result['userToken'],$this->loginTime);
            Cookie::set(md5('userInfo'),$result['uid'],$this->loginTime);
            Cookie::set(md5('userInfoBase'),$result,$this->loginTime);
            return true;
        }catch (\Exception $e){
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 获取密码加密
     * @return string
     */
    private function getPassword(){
        return md5(md5($this->prefix.$this->password).$this->prefix);
    }

    public function getLoginKey(){

        return md5($this->loginKey);
    }

    /**
     * 验证账号是否登录状态
     * @return bool
     */
    public function isLogin(){
        $userToken = Cookie::get($this->getLoginKey());
        if(!$userToken || !isset($userToken)){

            return false;
        }
        $loginStatus = cache($userToken);
        if(!$loginStatus || !isset($loginStatus)){

            return false;
        }
        return true;
    }


    public function webOutLogin(){
        $userToken =  Cookie::get($this->getLoginKey());
        Cookie::set($this->getLoginKey(),null);
        Cache::set($userToken,null);
        Session::set('login_uid',null);
        Session::set('userInfo',null);
        return true;
    }


    /**
     * 验证账号是否多点登录
     * @return array|string
     * @throws \Exception
     */
    public function validateLogin(){
        $userToken = Cookie::get($this->getLoginKey());
        $model = new validateLogin();
        $uid = Session::get('login_uid');
        $uid = empty($uid)?Cookie::get(md5('userInfo')):$uid;
        $model->setArgs('userToken',$userToken);
        $model->setArgs('loginId',$uid);
        $result = (new TopClient($model))->getSuccessData();
        return $result;
    }

    /**
     * @return array|string
     * @throws \Exception
     */
    public function weiXinLogin(){
        try{
            $model = new weiXinLogin();
            if(empty($this->openId)){
                throw new BlException('未获取到openId');
            }
            $model->setArgs('openId',$this->openId);
            $model->setArgs('password',$this->getPassword());
            $model->setArgs('loginIp','127.0.0.1');
            $result = (new TopClient($model))->getSuccessData();
            Session::set('login_uid',$result['uid']);
            Session::set('userInfo',$result);
            if(!isset($result['userToken']) || empty($result['userToken'])){
                throw new Exception('前端登录失败');
            }
            cache($result['userToken'],'loginIng',$this->loginTime);
            Cookie::set($this->getLoginKey(),$result['userToken'],$this->loginTime);
            Cookie::set(md5('userInfo'),$result['uid'],$this->loginTime);
            Cookie::set(md5('userInfoBase'),$result,$this->loginTime);
            return true;
        }catch (\Exception $e){
            throw new Exception($e->getMessage());
        }
    }

}